<?php
require_once(dirname(__FILE__).'/Abstract.php');
class Message extends AbstractPatientController {
/**
 * Message
 * 
 * Allows user to manage his messages like writing, reading, replying or deleting
 * 
 * @package Seowebd : Medic - Online
 * @author Seowebd
 * @copyright 2014
 * @version $Id$
 * @access public
 */

	public function indexAction() 
    {
        $this->inboxAction();
    }
    
    public function inboxAction()
    {
    	$d = array();
        $d['title'] = $this->l('My incomming messages');
        $this->load->model('Metier/Consultation/ConsultationMetier');
        
        $d['messages'] = $this->ConsultationMetier->getMessagesToPatient($this->session->userdata('id_user'), array());

        $d['title_for_layout'] = $this->l('Inbox messages.');
        $d['menu'] = 'messages';
        $d['page_name'] = 'messages_list';
        
        $this->load->view('Templates/Layouts/Patient/header', $d);
        $this->load->view('Patient/Message/Inbox', $d);
        $this->load->view('Templates/Layouts/Patient/footer', $d);
    }

    public function readAction($id_message = NULL)
    {
        $d = array();
        $d['title'] = $this->l('Message from');
        $this->load->model('Metier/Consultation/ConsultationMetier');
        
        $d['message'] = $this->ConsultationMetier->getMessageByIdFromDoctor($id_message, array());
        
        $d['title_for_layout'] = $this->l('Read message.');
        $d['menu'] = 'messages';
        $d['page_name'] = 'read_message';
        
        $this->load->view('Templates/Layouts/Patient/header', $d);
        $this->load->view('Patient/Message/Read', $d);
        $this->load->view('Templates/Layouts/Patient/footer', $d);
    }

    public function replyAction($id_message = NULL)
    {
        $d = array();
        $d['title'] = $this->l('Reply to');
        $this->load->model('Metier/Consultation/ConsultationMetier');
        if ($this->input->post('btn_submit') !== false){
             // Initialized the form validation
            $config = array(
               array(
                     'field'   => 'message', 
                     'label'   => $this->l('First name'), 
                     'rules'   => 'trim|required|xss_clean'
                  ),
               array(
                     'field'   => 'context', 
                     'label'   => $this->l('Context'), 
                     'rules'   => 'trim|required|xss_clean'
                  ),
               array(
                     'field'   => 'id_consultation', 
                     'label'   => $this->l('Consultation'), 
                     'rules'   => 'trim|required|xss_clean'
                  ),
            );
            $this->form_validation->set_rules($config);
            if ($this->form_validation->run() == FALSE ){
                $this->session->set_flashdata('errorMessage', $this->l('An error occured. Please check filled data and try again.'));
            } else {
                $this->load->model('ORM/Consultation/ConsultationMessageORM');
                // Initialize the new record
                $record = array(
                    'message'        => $this->input->post('message'),
                    'context'        => $this->input->post('context'),
                    'id_consultation'=> $this->input->post('id_consultation'),                   
                    // Automatic fill
                    'sender'         => 'patient',                             
                    'readed'         => '0',                           
                    'deleted'        => '00',
                    'date_add'       => @date('Y-m-d H:i:s'),
                );
                // Create the new message
                if ($this->ConsultationMessageORM->save($record)) {
                    // Redirect to the sign in form
                    redirect('Patient/Message/inbox');
                    die();
                } else {
                    $this->session->set_flashdata('errorMessage', $this->l('An error occured while saving your message. Please try again.'));
                }
            }
        }
        $d['message'] = $this->ConsultationMetier->getMessageByIdFromDoctor($id_message, array());
        
        $d['title_for_layout'] = $this->l('Reply to a message.');
        $d['menu'] = 'messages';
        $d['page_name'] = 'reply_message';
        
        $this->load->view('Templates/Layouts/Patient/header', $d);
        $this->load->view('Patient/Message/Reply', $d);
        $this->load->view('Templates/Layouts/Patient/footer', $d);
    }
}

